
const { Op, where } = require("sequelize");
const Photo = require("../model/photo.js");
const PhotoAlbum = require("../model/PhotoAlbum.js");
let { deleteList, deleteimg } = require("../util/qiniuUpload.js");
const { resolve } = require("path");
class Photoes {
    /* 添加相册 */
    async addaumbles(album_name, description) {
        let res = await PhotoAlbum.create({ album_name, description })
        if (res) {
            return {
                code: 1,
                msg: "添加成功",
                data: res.dataValues
            }
        } else {
            return {
                code: 0,
                msg: "添加失败"
            }
        }
    }
    /* 获取单个相册 */
    async getoneaumble(aumble_id) {
        let res = await PhotoAlbum.findOne({
            where: {
                id: aumble_id
            }
        })
        if (res) {
            return {
                code: 1,
                msg: "获取成功",
                data: res.dataValues
            }
        } else {
            return {
                code: 0,
                msg: "获取失败"
            }
        }
    }
    /* 修改相册图片 */
    async subfilename(aumble_id, file_name) {
        let res = await PhotoAlbum.update({ album_cover: file_name }, {
            where: {
                id: aumble_id
            }
        })

        if (res[0] > 0) {
            return {
                code: 1,
                data: res,
                msg: "修改成功"
            }
        } else {
            return {
                code: 0,
                msg: "修改失败"
            }
        }
    }
    /* 编辑相册 */
    async editaumbleinfo(id, album_name, description, album_cover = "") {
        try {
            let res
            if (album_cover == "") {
                res = await PhotoAlbum.update({ album_name, description }, {
                    where: {
                        id
                    }
                })
            } else {
                res = await PhotoAlbum.update({ album_name, description, album_cover }, {
                    where: {
                        id
                    }
                })

            }

            if (res[0] > 0) {
                return {
                    code: 1,
                    msg: "修改成功"
                }
            } else {
                return {
                    code: 0,
                    msg: "修改失败"
                }
            }
        } catch (error) {
            console.log(error);

        }
    }
    /* 获取数量 */
    async getaumblecount() {
        let res = await PhotoAlbum.count();
        return res;
    }
    /* 分页获取相册  */
    async getaumbleinfo(page, pageSize) {
        try {
            page = (typeof page == "string") ? parseInt(page) : page;
            pageSize = (typeof pageSize == "string") ? parseInt(pageSize) : pageSize;
            let { count, rows } = await PhotoAlbum.findAndCountAll({
                offset: (page - 1) * pageSize,
                limit: pageSize,
                attributes: { exclude: ['updatedAt', "createdAt"] },
                order: [['createdAt', 'DESC']]
            })
            let data = rows.map((item) => {
                return item.dataValues
            })
            if (data.length > 0) {
                return {
                    code: 1,
                    msg: "获取成功",
                    data
                }
            } else {
                return {
                    code: 0,
                    msg: "暂无数据",
                }
            }
        } catch (error) {
            console.log(error);
        }
    }
    /* 相册添加图片 */
    async addaumblepic(id, piclist) {
        let res = await Photo.bulkCreate(piclist);
        if (res) {
            return {
                code: 1,
                msg: "添加成功"
            }
        } else {
            return {
                code: 0,
                msg: "添加失败"
            }
        }
    }
    /* 相册id获取图片 */
    async getaumblepic(album_id, page, pageSize) {
        page = (typeof page == "string") ? parseInt(page) : page;
        pageSize = (typeof pageSize == "string") ? parseInt(pageSize) : pageSize;
        let res = await Photo.findAll({
            offset: (page - 1) * pageSize,
            limit: pageSize,
            attributes: { exclude: ['updatedAt', "createdAt", "status"] },
            where: {
                album_id
            }
        })
        let data = res.map(item => {
            return item.dataValues
        })
        if (data.length > 0) {
            return {
                code: 1,
                msg: "获取成功",
                data
            }
        } else {
            return {
                code: 0,
                msg: "暂无数据",
                data: []
            }
        }
    }
    /* 获取图片的数量 */
    async getaumblepicnum(album_id) {
        let res = await Photo.count({
            where: {
                album_id
            }
        })
        if (res) {
            return {
                code: 1,
                msg: "获取成功",
                data: res
            }
        } else {
            return {
                code: 0,
                msg: "暂无数据",
                data: 0
            }
        }
    }
    /* 批量删除 */
    async deletePicserve(album_id, todelectIdlist) {
        let imglist = []
        let fleg = false
        let deletedata = todelectIdlist.map(item => {
            imglist.push(item.url)
            return item.url.split("/").pop()
        })
        try {
            fleg = await new Promise((resolve, reject) => {
                deleteList(deletedata, async function (err, respBody, respInfo) {
                    if (err) {
                        reject(true)
                    } else {
                        if (parseInt(respInfo.statusCode / 100) == 2) {
                            respBody.forEach(function (item) {
                                if (item.code == 200) {
                                    console.log(item.code + "\tsuccess");
                                } else {
                                    reject(true)
                                }
                            })
                        } else {
                            reject(true)
                        }
                    }
                    if (!fleg) {
                        let res = await Photo.destroy({
                            where: {
                                album_id,
                                url: {
                                    [Op.in]: imglist
                                }
                            }
                        })
                        res ? resolve(false) : reject(true)
                    } else {
                        reject(true)
                    }
                })
            })
        } catch (error) {
            console.log(error);
        }
        if (!fleg) {
            console.log("成功");
            return {
                code: 1,
                msg: "删除成功"
            }
        } else {
            console.log("失败");
            return {
                code: 0,
                msg: "删除失败"
            }
        }
    }
    /* 删除相册 */
    async delectaumble(album_id, album_cover) {
        let cover = album_cover.split("/").pop()
        let fleg
        let fleg1
        let fleg2
        let res = await Photo.findAll({
            attributes: { exclude: ['updatedAt', "createdAt", "status"] },
            where: {
                album_id
            }
        })
        let deletedata = res.map(item => {
            return item.dataValues.url.split("/").pop()
        })
        try {
            fleg1 = new Promise((resolve, reject) => {
                if (deletedata.length <= 0) {
                    resolve(false)
                } else {
                    deleteList(deletedata, async function (err, respBody, respInfo) {
                        if (err) {
                            console.log(err);
                            reject(true)
                        } else {
                            if (parseInt(respInfo.statusCode / 100) == 2) {
                                respBody.forEach(function (item) {
                                    if (item.code == 200) {
                                        console.log(item.code + "\tsuccess");
                                    } else {
                                        reject(false)
                                    }
                                })
                            } else {
                                reject(true)
                            }
                        }
                        let res = await Photo.destroy({
                            where: {
                                album_id,
                            }
                        })
                        res ? resolve(false) : reject(true)
                    })
                }

            })
            fleg2 = new Promise((resolve, reject) => {
                deleteimg(cover, async (err, respBody, respInfo) => {
                    if (err) {
                        console.log(err);
                        reject(true)
                    } else {
                        if (respInfo.statusCode == 200) {
                            let ress = await PhotoAlbum.destroy({
                                where: {
                                    id: album_id
                                }
                            })
                            if (ress) {
                                resolve(false)
                            }
                        }
                    }
                })
            })
        } catch (error) {
            console.log(error);
        }
        fleg = await Promise.all([fleg1, fleg2]);

        console.log("fleg", fleg);
        let isok = fleg.every(item => item == false);
        if (isok) {
            return {
                code: 1,
                msg: '删除成功'
            }
        } else {
            return {
                code: 0,
                msg: '删除失败'
            }
        }
    }
}
module.exports = new Photoes(); //singleton